import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:osc/data/repository/repository.dart';
import 'package:osc/common/osc_user_manager.dart';
import 'package:osc/models/entity/osc_soft_banner_entity.dart';
import 'package:osc/models/pop_result/pop_result.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/res/strings.dart';
import 'package:osc/ui/pages/account/login_page.dart';
import 'package:osc/ui/pages/project/project_detail_page.dart';
import 'package:osc/ui/widgets/image_loader.dart';
import 'package:osc/ui/widgets/small_widgets.dart';
import 'package:osc/utils/navigator_util.dart';
import 'package:osc/utils/pd_util.dart';

///软件
class ProjectBanner extends StatefulWidget {

  const ProjectBanner({Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new ProjectBannerState();
  }
}

class ProjectBannerState extends State<ProjectBanner> with AutomaticKeepAliveClientMixin<ProjectBanner> {
  ProjectBannerState();

  HttpRepository httpRepository;


  double paddingChildVertical = Dimens.gap_view_normal;


  List<OscSoftBanner> bannerList;
  int size = 0;


  @override
  void initState() {
    super.initState();
    httpRepository = HttpRepository();
    refresh();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Container(
      color: Colors.white,
      height: Dimens.view_soft_author_height,
      width: ScreenUtil.getInstance().screenWidth,
      child: ListView.builder(
          shrinkWrap: true,
          scrollDirection: Axis.horizontal,
          itemCount: size,
          itemBuilder: (BuildContext context, int index){
            return buildBanner(bannerList.elementAt(index), index);
          }
      ),
    );
  }


  buildBanner(OscSoftBanner banner, int index){
    return GestureDetector(
      onTap: (){
//        jumpToPageForResult(WebScaffold(title: banner.name, url: banner.href,));
        jumpToPageForResult(ProjectDetailPage(id: banner.objId,));
      },
      child: Container(
        margin: EdgeInsets.only(right: Dimens.gap_view_bigger, left: index==0?Dimens.gap_view_bigger:0, top: Dimens.gap_view_bigger, bottom: Dimens.gap_view_bigger),
        width: ScreenUtil.getInstance().screenWidth*3/5,
        child: Stack(
          fit: StackFit.expand,
          children: <Widget>[
            ClipRRect(
              borderRadius: BorderRadius.circular(Dimens.view_radius_size_small),
              child: ImageLoader(url: banner.img, fit: BoxFit.cover,),
            ),
            ClipRRect(
              borderRadius: BorderRadius.circular(Dimens.view_radius_size_small),
              child: Container(
                color: Colors.black38,
              ),
            ),
            Positioned(
              bottom: 0,
              child: Padding(
                padding: EdgeInsets.all(Dimens.gap_view_bigger),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    SmallWidgets.buildText(banner.name, Colors.white, Dimens.font_sp14, fontWeight: FontWeight.bold),
                    SmallWidgets.buildText(banner.detail, Colors.white, Dimens.font_sp12),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }


  buildText(String data, Color color, double fontSize) {
    return SmallWidgets.buildText(data, color, fontSize);
  }

  refresh() async{
    bannerList?.clear();
    await loadMore();
  }


  loadMore() async {
    await new HttpRepository().getProjectBanner().then((resp) {
      OscSoftBannerEntity entity = resp.result;
      bannerList = entity?.data;
      if(bannerList != null){
        size = bannerList.length;
      }
    });

    if(mounted){
      setState(() {});
    }
  }

  jump2PageForResult(Widget page, {bool checkLogin = false}) {
    if(checkLogin){
      if(OscUserManager.getInstance().isLogin()){
        jumpToPageForResult(page);
      } else {
        jumpToPageForResult(LoginPage());
        showTips(StringIds.userNoLogin);
      }
    } else {
      jumpToPageForResult(page);
    }
  }
  jumpToPageForResult(Widget page) {
    NavigatorUtil.pushPageForResult<PopResult>(context, page).then((result) {
      if (result != null) {
        switch (result.id) {
          case PopResult.ID_LOGIN:
            break;
        }
      }
    });
  }

  showTips(String msg){
    PdUtil.showToast(msg: msg);
  }

  @override
  bool get wantKeepAlive => true;
}

class MyClipper extends CustomClipper<Rect>{
  final Offset offset;
  MyClipper(this.offset);

  @override
  Rect getClip(Size size) {
    return Rect.fromCircle(center: offset, radius: Dimens.view_radius_size_smaller);
  }

  @override
  bool shouldReclip(CustomClipper<Rect> oldClipper) {
    return false;
  }

}